export default {
  barChart(element) {
    let myChart = echarts.init(document.getElementById(element))
    let chartOption = {
      title: {text: "", x: 'center'},
      // legend: {show:false},
      tooltip: {trigger: 'axis', axisPointer: {type: 'shadow'},},
      grid:{bottom:20 , right:0, left:10,top:20,containLabel: true},
      color:new echarts.graphic.LinearGradient(0, 0, 0, 1,
        [{offset: 0, color: '#4E2DF2'}, {offset: 1, color: '#FF3C00'}]
      ),
      xAxis: {
        type: 'category',
        splitLine: {show: false},
        axisLine: {
          lineStyle:{
            color:'#1A2C66'
          }
        },
        axisLabel: {textStyle: {color: '#BFCFEF', fontSize: '12'}},
        data: [],
      },
      yAxis:{show:false},
      series: []
    }
    myChart.setOption(chartOption);
    // window.addEventListener("resize", () => {
    //  myChart.resize()
    // })
    return myChart
  },
  setLineOptionChart(chart,formatter,xData,yData,sName,lineColor,shadowColor,type){
    let series=[],legendData=[]
    if(Array.isArray(yData)){
      series=[this.setSeries(yData,sName,lineColor,shadowColor,type)]
    }else{
      let keys=Object.keys(yData)
      for(let i=0;i<keys.length;i++){
        let o=this.setSeries(yData[keys[i]],sName[keys[i]],lineColor[keys[i]],shadowColor[keys[i]],type)
        series.push(o)
        legendData.push({name:sName[keys[i]],
          textStyle:{color:'#BFCFEF',fontSize:12}
        })
      }
    }
    chart.setOption({
      legend: {top:0,data:legendData,icon:'roundRect'},
      tooltip: {trigger: 'axis', formatter:formatter},
      xAxis:{type: 'category',data:xData},
      series:series
    })
  },
  setSeries(yData,sName,lineColor,shadowColor,type){
    return {
      name: sName,type:(type?type:'line'),data:yData,symbolSize: 6,symbol:'circle', smooth:false,//false实线 true圆滑
        areaStyle: {
        normal: {
          color: {x: 0, y: 0, x2: 0, y2: 1,
            colorStops: [
            {offset: 0, color: shadowColor[0]},
            {offset: 0.6,color: shadowColor[1]}
          ],
            globalCoord: false
          }
        }
      },
      itemStyle: {
        normal: {color: lineColor}
      }
    }
  },
  pieChart(element) {
    let myChart=echarts.init(document.getElementById(element))
    let chartOption = {
      theme: '',
      event: [
        {
          type: "click",
          cb: function (res) {
          }
        }
      ],
      title: {text: '', subtext: '', x: 'center'},
      color: ['#00AAFF', '#FFAA00','#F83AED','#39F372','#FF7531','#FCFA32','#00FFEA','#2364E8'],
      tooltip: {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c}"
      },
      legend: {
        // orient: 'vertical',
        // left: 'left',
        // data: ['累计用电量','昨日用电量']
        show: false
      },
      textStyle:{fontSize:16, fontWeight:900},
      series: [{
        name: '',
        type: 'pie',
        // startAngle: 360,
        radius: ['70%', '36%'],
        // center: ['60%', '50%'],
        hoverOffset:-2,
        // minShowLabelAngle:360,
        label: {
          normal: {
            show: true,
            position: 'right',
            textStyle:{fontSize: 12, color:'#f2f2f2',fontWeight:400}
          }
        },
        labelLine: {
          lineStyle: {color: '#02E6EF'},
          smooth: 0.2,
          length: 36,
        },
        data: [
          {value: 1820, name: '累计用电量'},
          {value: 720, name: '昨日用电量'}
          ],
        itemStyle: {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }]
    }
    myChart.setOption(chartOption)
    return myChart
  }
}
